package com.dmall.opening.up;

import com.dmall.opening.up.core.OrderRepository;
import com.dmall.opening.up.core.Request;
import com.dmall.opening.up.records.OrderRecord;
import com.dmall.opening.up.records.util.Lists;
import com.dmall.opening.up.records.util.Maps;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application-context.xml")
@TestPropertySource("classpath:application.properties")
@Sql(scripts = "classpath:cleanup.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
public class BaseTestCase extends TestCase {

    protected static final Long FIXED_USER_ID = 1L;

    @Resource
    protected OrderRepository orders;

    @Before
    public void setUp() {}

    @Test
    public void empty() {}

    protected OrderRecord create_own_order() {
        return create_order("OWN");
    }

    protected OrderRecord create_third_party_order() {
        return create_order("THIRD_PARTY");
    }

    protected OrderRecord create_order(String referer) {
        return (OrderRecord) orders.create(Request.of(Maps.of(
                "user", Maps.of(
                        "id", FIXED_USER_ID
                ),
                "referer", referer,
                "ext", Maps.of(
                        "remark", "something"
                ),
                "items", Lists.newArrayList(
                        Maps.of(
                                "code", "A001"
                        ),
                        Maps.of(
                                "code", "B001"
                        )
                )
        )));
    }
}
